//
//  FilterFooterView.m
//  BDD_TWO
//
//  Created by XTW on 2023/6/9.
//

#import "FilterFooterView.h"
#import "StoryMakeFooterFilterCell.h"
#import "ImageUtil.h"
#import "ColorMatrix.h"

@interface FilterFooterView()<UICollectionViewDataSource, UICollectionViewDelegate> {
    StoryMakeFooterFilterCell *selectCell;
    NSArray                   *currentImages;
    UIImage                   *currentImage;
    NSInteger                 currentIndex;
}

@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (nonatomic, strong) UICollectionView   *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSArray <UIImage *> *dataArray;
@property (nonatomic, strong) NSArray <NSString *> *filterNameArray;
@end

@implementation FilterFooterView

- (id)initItem:(NSArray *)images {
    self = [[[NSBundle mainBundle] loadNibNamed:@"FilterFooterView" owner:self options:nil] lastObject];
    if (self) {
        currentImages = images;
        [self initView];
    }
    return self;
}

- (id)initItemImage:(UIImage *)image withIndex:(NSInteger)index {
    self = [[[NSBundle mainBundle] loadNibNamed:@"FilterFooterView" owner:self options:nil] lastObject];
    if (self) {
        currentImage = image;
        currentIndex = index;
        [self initView];
    }
    return self;
}

- (void)initView {
    [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE)];
    [self.bottomView setFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE, SCREEN_WIDTH_DEVICE, 131+IS_iPhoneX_Bottom)];

    [UIView animateWithDuration:0.3 animations:^{
        [self.bottomView setFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE - 131 - IS_iPhoneX_Bottom, SCREEN_WIDTH_DEVICE, 131+IS_iPhoneX_Bottom)];
    }];
    [self.bottomView addSubview:self.collectionView];
    
    self.filterNameArray = [NSArray arrayWithObjects:@"Normal", @"LOMO", @"黑白", @"哥特", @"复古", @"锐化", @"淡雅", @"酒红", @"清宁", @"浪漫", @"光晕", @"蓝调", @"梦幻", @"夜色",nil];
    NSMutableArray *mutArr = [NSMutableArray array];
    for (int i = 0; i < 14; i++) {
        if (currentImage) {
            [mutArr addObject:[self getFilterUtil:currentImage withIndex:i]];
        } else {
            [mutArr addObject:[self getFilterUtil:[currentImages firstObject] withIndex:i]];
        }
    }
    self.dataArray = mutArr;
    [self.collectionView reloadData];
}

- (IBAction)savePress {
    [self removeFromSuperview];
}

- (IBAction)removePress {
    if (self.SureHandle) {
        self.SureHandle(currentImages);
    }
    if (self.SureOnlyHandle) {
        self.SureOnlyHandle(currentImage, currentIndex);
    }
    [self removeFromSuperview];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    StoryMakeFooterFilterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[StoryMakeFooterFilterCell identifierForReuseCell] forIndexPath:indexPath];
    if (indexPath.section >= self.dataArray.count) {
        return cell;
    }
    cell.imageView.image = self.dataArray[indexPath.row];
    cell.filterNameLabel.text = self.filterNameArray[indexPath.row];
    if (!selectCell && indexPath.row == 0) {
        cell.selected = YES;
        selectCell = cell;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    StoryMakeFooterFilterCell *cell = (StoryMakeFooterFilterCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (selectCell != cell) {
        selectCell.selected = NO;
        cell.selected = YES;
        selectCell = cell;
    }
    
    if (self.SureHandle) {
        NSMutableArray *mutArr = [NSMutableArray array];
        for (int i = 0; i < currentImages.count; i++) {
            UIImage *image = [currentImages objectAtIndex:i];
            [mutArr addObject:[self getFilterUtil:image withIndex:indexPath.row]];
        }
        self.SureHandle(mutArr);
    }
    if (self.SureOnlyHandle) {
        self.SureOnlyHandle(cell.imageView.image, currentIndex);
    }
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 100) collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[StoryMakeFooterFilterCell class]
            forCellWithReuseIdentifier:[StoryMakeFooterFilterCell identifierForReuseCell]];
    }
    
    return _collectionView;
}

-(UICollectionViewFlowLayout *)flowLayout {
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = CGSizeMake(50, 75);
        _flowLayout.sectionInset = UIEdgeInsetsMake(0, 16, 24, 16);
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    
    return _flowLayout;
}

- (UIImage *)getFilterUtil:(UIImage *)image withIndex:(NSInteger)index {
    switch (index) {
        case 1:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_lomo];
            break;
        case 2:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_heibai];
            break;
        case 3:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_huajiu];
            break;
        case 4:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_gete];
            break;
        case 5:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_ruise];
            break;
        case 6:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_danya];
            break;
        case 7:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_jiuhong];
            break;
        case 8:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_qingning];
            break;
        case 9:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_langman];
            break;
        case 10:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_guangyun];
            break;
        case 11:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_landiao];
            break;
        case 12:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_menghuan];
            break;
        case 13:
            return [ImageUtil imageWithImage:image withColorMatrix:colormatrix_yese];
            break;
        default:
            return image;
            break;
    }
}
@end
